
package com.chuangan.releasetask.Utils;

import net.sf.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @Author: zihua
 * @Date: 2020/7/10 16:14
 * @Version 1.0
 */
public class JsapiTicketUtil {

    private static final String APPID = "wx7abbe52bb47722a6";
    private static final String APPSECRET = "34538ee2d430047f115e76d7662d4140";



    public static String sendGet(String url, String charset, int timeout)
    {
        String result = "";
        try
        {
            URL u = new URL(url);
            try
            {
                URLConnection conn = u.openConnection();
                conn.connect();
                conn.setConnectTimeout(timeout);
                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset));
                String line="";
                while ((line = in.readLine()) != null)
                {

                    result = result + line;
                }
                in.close();
            } catch (IOException e) {
                return result;
            }
        }
        catch (MalformedURLException e)
        {
            return result;
        }

        return result;
    }

        public  static String getAccessToken(){

        //String appid=wechatAccountConfig.getMpAppId();//应用ID
        //String appid = "wx7abbe52bb47722a6";
        //String appSecret=wechatAccountConfig.getMpAppSecret();//(应用密钥)
        //String appSecret = "34538ee2d430047f115e76d7662d4140";
        String url ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+APPID+"&secret="+APPSECRET+"";
        String backData=sendGet(url, "utf-8", 10000);
        String accessToken = (String) JSONObject.fromObject(backData).get("access_token");
        return accessToken;
    }


        public  static String getJSApiTicket(){
        //获取token
        String acess_token= getAccessToken();
        System.out.println(acess_token);
        String urlStr = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+acess_token+"&type=jsapi";
        String backData=sendGet(urlStr, "utf-8", 10000);
        String ticket = (String) JSONObject.fromObject(backData).get("ticket");
        return  ticket;

    }

    public static void main(String[] args) {
        String jsapiTicket = JsapiTicketUtil.getJSApiTicket();
        System.out.println("调用微信jsapi的凭证票为："+jsapiTicket);
        String maxOrderSeq = "202007130001";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String today = sdf.format(new Date());
        if(maxOrderSeq.startsWith(today))
        {
            BigDecimal maxSeq=new BigDecimal(maxOrderSeq);
            BigDecimal one=new BigDecimal("1");
            maxSeq = maxSeq.add(one);
            System.out.println("maxSeq:"+maxSeq.toPlainString());
        }
        else
        {
            maxOrderSeq = today + "0001";
            System.out.println("maxOrderSeq:"+maxOrderSeq);
        }


    }



}

